package leetcode_401_500;

public class LeeCode_482 {
    public static void main(String[] args) {
//        System.out.println(licenseKeyFormatting("5F3Z-2e-9-w", 4));
        System.out.println(licenseKeyFormatting("---", 4));
    }
    private static String licenseKeyFormatting(String s, int k) {
        StringBuilder ss = new StringBuilder(s);
        for (int i = 0; i < ss.length(); i++) {
            if (ss.charAt(i) <= 'z' && ss.charAt(i) >= 'a'){
                ss.replace(i, i + 1, (char)(ss.charAt(i) - 'a' + 'A') + "");
            }
        }
        s = ss.toString();
        String[] split = s.split("-");
        if (split.length == 0){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String s1 : split) {
            sb.append(s1);
        }
        int len = sb.length();
        StringBuilder ans = new StringBuilder();
        if (len % k == 0){
            int i = 0;
            while (i < len) {
                ans.append(sb.subSequence(i, i + k));
                ans.append("-");
                i += k;
            }
        }else {
            int i = len % k;
            ans.append(sb.substring(0, i));
            ans.append("-");
            while (i < len){
                ans.append(sb.substring(i, i + k));
                ans.append("-");
                i += k;
            }
        }
        ans.replace(ans.length() - 1, ans.length(), "");
        return ans.toString();
    }
}
